<template>
  <el-form
    :inline="true"
    :model="form"
    ref="form"
    size="mini"
    style="margin-bottom: -18px;">
    <el-form-item label="店铺名称" prop="keywords">
      <el-input
        v-model="form.shopName"
        prefix-icon="el-icon-search"
        placeholder="输入店铺名称"
        @keyup.enter.native="handleFormSubmit(true)"
        :clearable="true"/>
    </el-form-item>

    <el-form-item label="商家姓名" prop="account">
      <el-input
        v-model="form.name"
        prefix-icon="el-icon-search"
        placeholder="可输入商家姓名/企业法人姓名"
        @keyup.enter.native="handleFormSubmit(true)"
        :clearable="true"/>
    </el-form-item>

    <el-form-item label="商铺类型" prop="shop_type">
      <el-select
        v-model="form.shop_type"
        placeholder="请选择"
        style="width: 120px;"
        clearable>
        <el-option
          v-for="(item, index) in shopType"
          :key="index"
          :label="item.name"
          :value="item.code"/>
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        :disabled="loading"
        @click="handleFormSubmit(true)">查询</el-button>
    </el-form-item>

    <el-form-item>
      <el-button
        icon="el-icon-refresh"
        @click="handleFormReset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: {
    loading: {
      default: false
    },
    shopType: {
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        shop_name: undefined,
        name: undefined,
        shop_type: undefined
      }
    }
  },
  methods: {
    handleFormSubmit(isRestore = false) {
      let form = {}
      for (const index in this.form) {
        if (!Object.prototype.hasOwnProperty.call(this.form, index)) {
          continue
        }

        // 时间段处理
        if (index === 'time_period') {
          if (this.form[index] && this.form[index].length === 2) {
            form.begin_time = this.form[index][0].toUTCString()
            form.end_time = this.form[index][1].toUTCString()
          }

          continue
        }

        form[index] = this.form[index]
      }

      this.$emit('submit', form, isRestore)
    },
    handleFormReset() {
      this.form.time_period = null
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
  .more-filter {
    margin-bottom: -18px;
  }

  .more-filter >>> label {
    width: 82px;
  }

  .more-filter >>> .el-form-item__content {
    margin-left: 82px;
  }
</style>
